package com.example.openguass.controller;


import com.example.openguass.entity.CommonResult;
import com.example.openguass.entity.OrderDetail;
import com.example.openguass.entity.Orders;
import com.example.openguass.service.OrderDetailService;
import com.example.openguass.service.OrdersService;
import com.example.openguass.validation.Update;
import com.example.openguass.vo.*;
import io.swagger.annotations.Api;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xa
 * @since 2022-11-17
 */
@RestController
@CrossOrigin
@RequestMapping("/orders")
@Api(value = "OrdersController", tags = "订单接口")
public class OrdersController {
    private OrdersService ordersService;
    private OrderDetailService orderDetailService;
    @Autowired
    private void setOrdersService(OrdersService ordersService){
        this.ordersService = ordersService;
    }
    @Autowired
    private void setOrderDetailService(OrderDetailService orderDetailService){
        this.orderDetailService = orderDetailService;
    }


    @PostMapping("insert")
    public CommonResult insert(@RequestBody MakeOrderVO makeOrderVO){
        CommonResult result = new CommonResult().init();
        if (ordersService.insert(makeOrderVO.getList())){
            return result.success("创建订单成功",makeOrderVO.getList());
        }else {
            return (CommonResult) result.failCustom(-10086,"创建订单失败");
        }
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody @Validated({Update.class}) OrdersVO ordersVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            return (CommonResult) result.failIllegalArgument(bindingResult.getFieldErrors()).end();
        }
        if (ordersService.getById(ordersVO.getId()) == null){
            return (CommonResult) result.failCustom(-10086,"不存在当前订单");
        }
        if (ordersService.update(ordersVO)){
            return result.success("orderDetail",ordersVO);
        }else {
            return (CommonResult) result.failCustom(-10086,"更新订单失败");
        }
    }

    @GetMapping("/delete/{ordersId}")
    public CommonResult delete(@PathVariable String ordersId){
        CommonResult result = new CommonResult().init();
        if (ordersService.getById(ordersId) == null){
            return (CommonResult) result.failCustom(-10086,"该订单不存在");
        }
        if (ordersService.removeById(ordersId)){
            orderDetailService.removeByOrderId(ordersId);
            return (CommonResult) result.success();
        }else {
            return (CommonResult) result.failCustom(-10086,"删除订单失败");
        }
    }

    @GetMapping("/select/{ordersId}")
    public CommonResult select(@PathVariable String ordersId){
        CommonResult result = new CommonResult().init();
        if (ordersService.getById(ordersId) == null){
            return (CommonResult) result.failCustom(-10086,"该订单不存在");
        }
        Orders orders = ordersService.getById(ordersId);
        return result.success("orders",orders);
    }


    @PostMapping("findPage")
    public CommonResult findPage(@RequestBody OrdersPageVO ordersPageVO, BindingResult bindingResult){
        CommonResult result = new CommonResult().init();
        //参数验证
        if (bindingResult.hasErrors()) {
            result.failIllegalArgument(bindingResult.getFieldErrors()).end();
            return result;
        }
        return (CommonResult) result.success("page",ordersService.findPage(ordersPageVO)).end();
    }

    @GetMapping("/getMenu")
    public CommonResult getMenu(){
        CommonResult result = new CommonResult().init();
        return (CommonResult) result.success("menu",ordersService.getMenu()).end();
    }

    @GetMapping("/analysis")
    public CommonResult analysis(){
        CommonResult result = new CommonResult().init();

        //获取当天的数据
        result.success("today",ordersService.getAnalysisVO(LocalDateTime.of(LocalDate.now(), LocalTime.MIN)));
        //获取昨天数据s
        result.success("yesterday",ordersService.getAnalysisVO(LocalDateTime.of(LocalDate.now().plusDays(-1), LocalTime.MIN),LocalDateTime.of(LocalDate.now(), LocalTime.MIN)));
        //获取当月数据
        result.success("month",ordersService.getAnalysisVO(LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN)));
        //获取当年数据
        result.success("year",ordersService.getAnalysisVO(LocalDateTime.of(LocalDate.from(LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear())), LocalTime.MIN)));

        return result;
    }


}
